﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace DistributedSystems
{
    public class UtilsHTML
    {
        public static int SPEED = 30;

        //return minutes
        public static int CalculatorTime(int postCodeOrigin, int PostcodeDest)
        {
            float km = CalculatorDistance(postCodeOrigin, PostcodeDest);
            return (int)((km / SPEED) * 60);
        }
        //return km 
        public static float CalculatorDistance(int postCodeOrigin, int PostcodeDest)
        {
            try
            {
                string URL = "http://ultraventus.info/postalcode_distance/API.php?postalcode1={0}&postalcode2={1}";
                string fullURL = string.Format(URL, postCodeOrigin, PostcodeDest);

                WebRequest request = WebRequest.Create(fullURL);
                request.Proxy = null;
                WebResponse response = request.GetResponse();
                var datastream = response.GetResponseStream();
                StreamReader reader = new StreamReader(datastream);
                string strhtml = reader.ReadToEnd();
                return float.Parse(strhtml.Trim().ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }

        }
    }

}